package com.ruoyi.web.controller.ai;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ai.domain.AiWorkCollect;
import com.ruoyi.ai.domain.AiWorkHistory;
import com.ruoyi.ai.domain.bo.AIWorkLikeBo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ai.domain.AiWork;
import com.ruoyi.ai.service.IAiWorkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作品管理Controller
 *
 * @author tang
 * @date 2024-12-10
 */
@RestController
@RequestMapping("/ai/work")
public class AiWorkController extends BaseController
{
    @Autowired
    private IAiWorkService aiWorkService;

    /**
     * 查询作品管理列表
     */
    @PreAuthorize("@ss.hasPermi('ai:work:list')")
    @GetMapping("/list")
    public TableDataInfo list(AiWork aiWork)
    {
        startPage();
        List<AiWork> list = aiWorkService.selectAiWorkList(aiWork);
        return getDataTable(list);
    }

    /**
     * 导出作品管理列表
     */
    @PreAuthorize("@ss.hasPermi('ai:work:export')")
    @Log(title = "作品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiWork aiWork)
    {
        List<AiWork> list = aiWorkService.selectAiWorkList(aiWork);
        ExcelUtil<AiWork> util = new ExcelUtil<AiWork>(AiWork.class);
        util.exportExcel(response, list, "作品管理数据");
    }

    /**
     * 获取作品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ai:work:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId)
    {
        return success(aiWorkService.selectAiWorkByWorkId(workId));
    }

    /**
     * 新增作品管理
     */
    @PreAuthorize("@ss.hasPermi('ai:work:add')")
    @Log(title = "作品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody AiWork aiWork)
    {
        return toAjax(aiWorkService.insertAiWork(aiWork));
    }

    /**
     * 修改作品管理
     */
    @PreAuthorize("@ss.hasPermi('ai:work:edit')")
    @Log(title = "作品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody AiWork aiWork)
    {
        return toAjax(aiWorkService.updateAiWork(aiWork));
    }

    /**
     * 删除作品管理
     */
    @PreAuthorize("@ss.hasPermi('ai:work:remove')")
    @Log(title = "作品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds)
    {
        return toAjax(aiWorkService.deleteAiWorkByWorkIds(workIds));
    }

    @PreAuthorize("@ss.hasPermi('ai:collect:list')")
    @GetMapping("/listCollect")
    public TableDataInfo listCollect(AiWorkCollect aiWorkCollect)
    {
        startPage();
        List<AiWork> list = aiWorkService.selectAiWorkListByCollect(aiWorkCollect);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('ai:collect:add')")
    @Log(title = "作品收藏管理", businessType = BusinessType.INSERT)
    @PostMapping("/addCollect")
    public AjaxResult addCollect(@Validated @RequestBody AiWorkCollect aiWorkCollect)
    {
        return toAjax(aiWorkService.insertAiWorkCollect(aiWorkCollect));
    }
    @PreAuthorize("@ss.hasPermi('ai:collect:remove')")
    @Log(title = "作品收藏管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeCollect")
    public AjaxResult removeCollect(@Validated @RequestBody AiWorkCollect aiWorkCollect)
    {
        return toAjax(aiWorkService.deleteAiWorkCollect(aiWorkCollect));
    }
    @PreAuthorize("@ss.hasPermi('ai:history:list')")
    @GetMapping("/listHistory")
    public TableDataInfo listHistory(AiWorkHistory aiWorkHistory)
    {
        startPage();
        List<AiWork> list = aiWorkService.selectAiWorkListByHistory(aiWorkHistory);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('ai:history:add')")
    @Log(title = "作品访问管理", businessType = BusinessType.INSERT)
    @PostMapping("/addHistory")
    public AjaxResult addHistory(@Validated @RequestBody AiWorkHistory aiWorkHistory)
    {
        return toAjax(aiWorkService.insertAiWorkHistory(aiWorkHistory));
    }
    @PreAuthorize("@ss.hasPermi('ai:history:remove')")
    @Log(title = "作品访问管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeHistory")
    public AjaxResult removeHistory(@Validated @RequestBody AiWorkHistory aiWorkHistory)
    {
        return toAjax(aiWorkService.deleteAiWorkHistory(aiWorkHistory));
    }
    @PreAuthorize("@ss.hasPermi('ai:like:update')")
    @Log(title = "作品点赞管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateLike")
    public AjaxResult updateWorkLikeNumber(@Validated @RequestBody AIWorkLikeBo bo)
    {
        return toAjax(aiWorkService.updateAiWorkLikeNumber(bo.getWorkId(), bo.getNumber()));
    }
}
